package com.triones.api.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.triones.api.entity.AndroidVersionMgtDO;
import com.triones.api.mapper.AndroidVersionMgtMapper;
import com.triones.api.service.AndroidVersionMgtService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 安卓版本控制 -业务实现
 */
@Service
@RequiredArgsConstructor
public class AndroidVersionMgtServiceImpl implements AndroidVersionMgtService {

    private final AndroidVersionMgtMapper androidVersionMgtMapper;


    @Override
    public TableDataInfo<AndroidVersionMgtDO> selectPageList(AndroidVersionMgtDO dto, PageQuery pageQuery) {
        LambdaQueryWrapper<AndroidVersionMgtDO> lqw = new LambdaQueryWrapper<AndroidVersionMgtDO>();
        lqw.eq(AndroidVersionMgtDO::getDel, "0");
        lqw.eq(ObjectUtil.isNotEmpty(dto.getVersionNo()),  AndroidVersionMgtDO::getVersionNo, dto.getVersionNo());
        lqw.orderByDesc(AndroidVersionMgtDO::getUpdateAt);
        Page<AndroidVersionMgtDO> result = androidVersionMgtMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public AndroidVersionMgtDO selectById(Long id) {
        return androidVersionMgtMapper.selectById(id);
    }

    @Override
    public boolean insert(AndroidVersionMgtDO dto) {
        dto.setCreateAt(System.currentTimeMillis());
        dto.setUpdateAt(System.currentTimeMillis());
        return androidVersionMgtMapper.insert(dto) > 0;
    }

    @Override
    public boolean update(AndroidVersionMgtDO dto) {
        dto.setUpdateAt(System.currentTimeMillis());
        return androidVersionMgtMapper.updateById(dto) > 0;
    }

    @Override
    public boolean checkVersionNoUnique(AndroidVersionMgtDO dto) {
        boolean exist = androidVersionMgtMapper.exists(new LambdaQueryWrapper<AndroidVersionMgtDO>()
            .eq(AndroidVersionMgtDO::getVersionNo, dto.getVersionNo())
            .ne(ObjectUtil.isNotNull(dto.getId()), AndroidVersionMgtDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public AndroidVersionMgtDO getLast() {
        return androidVersionMgtMapper.selectOne(new LambdaQueryWrapper<AndroidVersionMgtDO>()
            .eq(AndroidVersionMgtDO::getDel, "0")
            .orderByDesc(AndroidVersionMgtDO::getUpdateAt).last("limit 1"));
    }

    @Override
    public void removeData(Long id) {
        AndroidVersionMgtDO androidVersionMgtDO = androidVersionMgtMapper.selectById(id);
        if (androidVersionMgtDO != null) {
            androidVersionMgtDO.setDel("1");
            androidVersionMgtMapper.updateById(androidVersionMgtDO);
        }
    }
}
